<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Topic extends Modules_Controller
{

	function __construct()
	{
		parent::__construct();

		$this->rules = array(
			"rule" => array(
				array(
					"field" => "title",
					"label" => lang('title'),
					"rules" => "trim|required|min_length[1]"
				)
				,array(
					"field" => "timeline",
					"label" => lang('time'),
					"rules" => "trim|strtotime"
				)
				,array(
					"field" => "content",
					"label" => lang('conent'),
					"rules" => "trim"
					// link_create tag 生成
				)
				,array(
					"field" => "photo",
					"label" => lang('photo'),
					"rules" => "trim"
				)
			)
		);

	}

    protected function _index_where(){
        $arr =array();
        if (isset($_GET['title'])) {
            $arr['like'] = array('title',$_GET['title']);
        }
        return $arr;
    }

    public function _edit_data()
    {
        $form=$this->input->post();
        $form['timeline'] = time();
        return $form;
    }
    // TODO:功能
    // 推荐 flag
    public function flag($key = FALSE){
        if (!$key AND $this->input->post('ids') ) {
            $key = explode(',',$this->input->post('ids'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_id'));
        }

        $where = FALSE;

        if ($this->input->get('c')) {
            $where = array('cid' => $this->input->get('c'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_col_id'));
        }

        $flag = $this->input->post('flag');

        if ($flag) {
            $flag=1;
        }else{
            $flag=0;
        }

        $msg = array('取消置顶','置顶成功');

        if (!isset($vdata['status'])) {
            if ($where) {
                $res = $this->model->flag($flag,$key,$where);
            }else{
                $res = $this->model->flag($flag,$key);
            }

            if ($res) {
                $vdata = array('status'=>1,'msg'=>$msg[$flag]);
                if (is_array($key)) {
                    $this->mlogs->add('flag',lang('modules_flag_id').$this->input->post('ids').lang('modules_flag_for').$flag);
                }else{
                    $this->mlogs->add('flag',lang('modules_flag_id').$key.lang('modules_flag_for').$flag);
                }
            }else{
                $vdata = array('status'=>0,'msg'=>lang('modules_flag_err_select'));
            }
        }

        if($this->input->is_ajax_request()){
            $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
        }else{
            $this->load->view('msg',$vdata);
        }
    }

}
